package com.example.smart_video.adapter;

import static com.example.smart_video.model.Constants.TENCENT_BUCKET_OFFICE_URL;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;


import com.example.cleandemo3.R;
import com.example.cleandemo3.databinding.ItemView01Binding;
import com.example.cleandemo3.databinding.ItemView02Binding;
import com.example.cleandemo3.databinding.ItemView03Binding;
import com.example.cleandemo3.databinding.ItemView06Binding;
import com.example.cleandemo3.databinding.ItemView12Binding;
import com.example.cleandemo3.databinding.ItemView13Binding;
import com.example.cleandemo3.databinding.ItemView15Binding;
import com.example.cleandemo3.databinding.ItemView16Binding;
import com.example.smart_video.model.json.PictureData;
import com.example.smart_video.model.json.VideoClassData;
import com.example.smart_video.model.json.VideoData;
import com.wink_172.library.model.BaseDataItem;

import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;


public class HBomeListAdapter extends com.wink_172.library.adapter.HomeListAdapter {

    /**
     * 列表样式
     */
    final static public int SCENE_DYNAMIC = 200;
    final static public int SCENE_SPACE = 202; //间距
    final static public int SCENE_SINGLE_ARROW = 203; //包含左图标，包含右图标，包含左文字，可进入二级界面
    final static public int SCENE_PLAY_LIST = 206; //主页状态
    final static public int SCENE_SELECT_CLASS_LIST = 212; //选择国家列表样式

    final static public int SCENE_TEXT_LIST = 213; //文案列表
    final static public int SCENE_THUMB_LIST = 215; //略缩图列表
    final static public int SCENE_IMAGE_LIST = 216; //图库列表

    public HBomeListAdapter(Context context, List<Object> dataList, RecyclerView listview) {
        super(context, dataList, listview);
    }

    public HBomeListAdapter.BaseViewsHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        View view = null;

        switch (viewType) {
            case SCENE_DYNAMIC:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view01, parent, false);
                return new TempleteViewsHolder(view);

            case SCENE_SPACE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view02, parent, false);
                return new SpaceViews01Holder(view);

            case SCENE_SELECT_CLASS_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view12, parent, false);
                return new SelectCountryViewsHolder(view);

            case SCENE_SINGLE_ARROW:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view03, parent, false);
                return new SingleArrowViewsHolder(view);

            case SCENE_PLAY_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view06, parent, false);
                return new HomeVideoViewsHolder(view);

            case SCENE_TEXT_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view13, parent, false);
                return new TextListViewsHolder(view);

            case SCENE_THUMB_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view15, parent, false);
                return new ThumbListViewsHolder(view);

            case SCENE_IMAGE_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view16, parent, false);
                return new ImageListViewsHolder(view);


            default:
                return null;
        }
    }


    private class TempleteViewsHolder extends BaseViewsHolder {

        private ItemView01Binding viewBinding;

        public TempleteViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {
            if (itemData instanceof BaseDataItem) {
                BaseDataItem item = (BaseDataItem) itemData;
                viewBinding.content.setText(getMContext().getResources().getString(R.string.app_name));
            }
        }
    }

    private class SpaceViews01Holder extends BaseViewsHolder {

        private ItemView02Binding itemView02Binding;

        public SpaceViews01Holder(View itemView) {
            super(itemView);
            itemView02Binding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {

        }
    }

    private class SingleArrowViewsHolder extends BaseViewsHolder {

        private ItemView03Binding itemView03Binding;

        public SingleArrowViewsHolder(View itemView) {
            super(itemView);
            itemView03Binding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {
            if (itemData instanceof BaseDataItem) {
                BaseDataItem item = (BaseDataItem) itemData;
                itemView03Binding.iconLeft.setImageResource(item.getRes());
                itemView03Binding.content.setText(item.getTitle());
            }
        }
    }

    private class ImageListViewsHolder extends BaseViewsHolder {

        private ItemView16Binding viewBinding;
        private ImageOptions options;

        public ImageListViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);


            ImageOptions.ParamsBuilder paramsBuilder = new ImageOptions.ParamsBuilder() {
                @Override
                public RequestParams buildParams(RequestParams params, ImageOptions options) {
                    params.setCacheSize(1024 * 1024 * 30);
                    return params;
                }
            };
            options = new ImageOptions.Builder()
                    .setUseMemCache(true)
                    .setFadeIn(true)
                    .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                    .setParamsBuilder(paramsBuilder)
                    .build();
        }

        @Override
        public void updateContent(Object itemData) {
            BaseDataItem item = (BaseDataItem) itemData;
            PictureData classData = (PictureData) item.getData();

            if(isListSelect()){
                viewBinding.select.setVisibility(View.VISIBLE);
            }else {
                viewBinding.select.setVisibility(View.GONE);
            }

            if(item.getSelect()){
                viewBinding.select.setActivated(true);

            }else {
                viewBinding.select.setActivated(false);
            }


            x.image().bind(viewBinding.image, classData.local_url, options);
        }
    }

    private class ThumbListViewsHolder extends BaseViewsHolder {

        private ItemView15Binding viewBinding;

        public ThumbListViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {

            BaseDataItem item = (BaseDataItem) itemData;
            VideoData classData = (VideoData) item.getData();

            x.image().bind(viewBinding.image, TENCENT_BUCKET_OFFICE_URL + classData.getThumbnail());


        }
    }

    private class TextListViewsHolder extends BaseViewsHolder {

        private ItemView13Binding viewBinding;

        public TextListViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {

            BaseDataItem item = (BaseDataItem) itemData;
            VideoData classData = (VideoData) item.getData();
            viewBinding.contentView.setText(classData.getContent());


        }
    }

    private class SelectCountryViewsHolder extends BaseViewsHolder {

        private ItemView12Binding viewBinding;

        public SelectCountryViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);
        }

        @Override
        public void updateContent(Object itemData) {

            BaseDataItem item = (BaseDataItem) itemData;
            VideoClassData classData = (VideoClassData) item.getData();
            viewBinding.contentView.setText(classData.getName());


            switch (classData.getId()) {
                case 4:
                    viewBinding.contentView.setTextColor(getMContext().getResources().getColor(R.color.color_pan1));
                    break;

                case 1:
                    viewBinding.contentView.setTextColor(getMContext().getResources().getColor(R.color.color_pan5));
                    break;

                default:
                    viewBinding.contentView.setTextColor(getMContext().getResources().getColor(R.color.color_orge_800));
                    break;
            }
        }
    }


    private class HomeVideoViewsHolder extends BaseViewsHolder {
        private ItemView06Binding viewBinding;
        private ImageOptions options;

        public HomeVideoViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);
            viewBinding.setOnClickListener(this);
            options = new ImageOptions.Builder()
                    .setUseMemCache(true)
//                    .setFadeIn(true)
                    .setCircular(true)
                    .build();

        }


        @Override
        public void onClick(View v) {
            super.onClick(v);

            switch (v.getId()) {

                case R.id.avater_view:
                case R.id.content_view:
                case R.id.title_view:
                    getCallback_m().onSendEvent(com.wink_172.library.model.Constants.EVENT_01);
                    break;

                case R.id.btn_create:
                    getCallback_m().onSendEvent(com.wink_172.library.model.Constants.EVENT_02);
                    break;

                case R.id.btn_sel_class:
                    getCallback_m().onSendEvent(com.wink_172.library.model.Constants.EVENT_03);
                    break;

            }
        }

        @Override
        public void updateContent(Object itemData) {
            BaseDataItem item = (BaseDataItem) itemData;
            VideoData videoData = (VideoData) item.getData();

            viewBinding.titleView.setText(videoData.getName());
            viewBinding.contentView.setText(videoData.getId() + " '" + videoData.getContent());
            setCallback_m(item.getCallback_m());


            String image = TENCENT_BUCKET_OFFICE_URL + videoData.getThumbnail();
            x.image().bind((ImageView) viewBinding.avaterView, image, options);

        }

    }

    private static final String TAG = "HomeListAdapter";
}